<?php


namespace Biz\service\advertisement\position;

use Biz\model\advertisement\MPositionRules;
use Biz\service\advertisement\AdUserProfileManagement;
use Doraemon\service\position\PositionApi;
use Doraemon\service\position\UserProfileModule;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Exception;

class BizPositionUserprofileEs extends BizPositionPageTpl
{
    const TPL_CONFIG_TPL_NAME = '用户画像es查询';


    public function getList(): array
    {
        $condition= $this->getRequestCondition();
        $module = $condition['module'] ?? 0;
        $ids = $condition['id'] ?? '';
        $show = $condition['ext'] ?? 0;
        $ids = $ids ? explode(',', $ids) : [];
        $rules = \Doraemon\service\position\PositionApi::activeRules($ids, $module);
        $count = 0;
        $uids = '没有分群条件';
        if ($ids && $rules) {
            foreach ($rules as $id => $rule) {
                UserProfileModule::debug();
                $count = UserProfileModule::generateEsCount($rule, false, true);
                if ($count > 5000 && !$show) {
                    $uids = 'over 5000 uid';
                } else {
                    $uids = UserProfileModule::getEsUidByRules($rule);
                    $uids = implode(",", $uids);
                }
            }

        }
        return [['count' => $count, 'uids' => $uids]];
    }

    public function getTotal(): int
    {
        return 0;
    }

    public function condition(): array
    {
        $fl = $this->getRequest()->input('fl', '');
        $module = $this->getRequest()->input('module', '');
        $ext = $this->getRequest()->input('ext', '');
        $id = $this->getRequest()->input('id', '');
        $condition = [];

        if ($fl) $condition['fl'] = $fl;
        if ($ext) $condition['ext'] = $ext;
        if ($id) $condition['id'] = $id;
        if (is_numeric($module)) $condition['module'] = $module;

        return $condition;
    }

    public function getGrid(Grid $grid)
    {
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disablePagination();
        $grid->disableActions();
        $grid->filter(function (Grid\Filter $filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->equal('module', '用户画像应用模块')->select(MPositionRules::$moduleMap);
                $filter->equal('id', '配置id')->placeholder('多个id使用 , 分隔');
            });
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->equal('ext', '查询所有uid')->select([1 => '是']);
            });
            $filter->expand();
        });
        $grid->column('count', '查询总数量')->width(100);
        $grid->column('uids', '查询用户id')->width(1200);
    }

    public function withInfoHtml(): array
    {
        $condition = $this->getRequestCondition();

        $logs = UserProfileModule::debugLogs();

        return $logs ? ["debug信息", implode("<br/>", $logs)] : [];
    }

    public function getForm(Form $form)
    {

    }



    /**
     * @throws Exception
     */
    public function save($input)
    {
    }

    public function delete($id): int
    {
        return 0;
    }


    public function getFormInfo($id)
    {

    }
}